/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prog38081.servlets;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Kyle
 */
public class DBMethods {
    
       public static ArrayList<CustomerOrder> GetAllCustomerOrders() {
           Connection conn = null;
           PreparedStatement pstmt = null;
           ResultSet rs = null;
           ArrayList<CustomerOrder> orders = new ArrayList<CustomerOrder>();
           
           try {
               
               conn = DBConnectionFactory.getConnection();
           
               String sql = "SELECT * FROM CustomerOrders";
               pstmt = conn.prepareStatement(sql);
               rs = pstmt.executeQuery();
               
               while (rs.next()) {
                   orders.add(new CustomerOrder( rs.getString("item"),
                                                 rs.getDouble("price"),
                                                rs.getDouble("discount"),
                                                rs.getDouble("taxes"),
                                                rs.getDouble("finalsale")));
               }
               
           } catch (Exception ex) {
               
           } finally {
               DBConnectionFactory.close(conn);
               DBConnectionFactory.close(pstmt);
               DBConnectionFactory.close(rs);
               
               return orders;
           }
       }
       
       public boolean addCustomerOrder(CustomerOrder order) {
           
           Connection conn = null;
           PreparedStatement stmt = null;
           int itemsAdded = -1;
           
           try {
               
               conn = DBConnectionFactory.getConnection();
               
               String sql = "INSERT INTO customerorders VALUES(?, ?, ?, ?, ?)";
               stmt = conn.prepareStatement(sql);
               
               stmt.setString(1, order.getItem());
               stmt.setDouble(2, order.getPrice());
               stmt.setDouble(3, order.getDiscount());
               stmt.setDouble(4, order.getTaxes());
               stmt.setDouble(5, order.getFinalsale());
               
               stmt.executeUpdate();
               itemsAdded = 1;
               
           } catch (Exception e) {
               System.out.println("Some Error: " + e.getMessage());
           } finally {
               DBConnectionFactory.close(conn);
               DBConnectionFactory.close(stmt);
           }
           return (itemsAdded > 0);
       }
}
